//
//  VersionViewController.m
//  Bigeyes
//
//  Created by 红沙尘 on 17/6/15.
//  Copyright © 2017年 uShareit. All rights reserved.
//

#import "VersionViewController.h"
#import "HSCVersionCell.h"

@interface VersionViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *versionArray;
    __weak IBOutlet UITableView *myTableView;
    NSMutableDictionary *showManagerDict;
}
@end

@implementation VersionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    showManagerDict = [[NSMutableDictionary alloc] init];
    [showManagerDict setObject:@"yes" forKey:@"0"];//第一个默认显示
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"VersionList" ofType:@"plist"];
    versionArray = [NSArray arrayWithContentsOfFile:path];
    
    
    [myTableView registerNib:[UINib nibWithNibName:@"HSCVersionCell" bundle:nil]
      forCellReuseIdentifier:@"HSCVersionCell"];
    myTableView.rowHeight = UITableViewAutomaticDimension;
    myTableView.estimatedRowHeight = 60;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return versionArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSString *key = [NSString stringWithFormat:@"%d",(int)section];
    if (!showManagerDict[key]) {
        return 0;
    }
    
    NSDictionary *dict = versionArray[section];
    return [dict[@"list"] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 45;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [self tableViewHeaderViewAtSection:section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HSCVersionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HSCVersionCell"];
    
    NSDictionary *dict = versionArray[indexPath.section];
    
    NSArray *array = dict[@"list"];
    
    [cell loadVersionDict:array[indexPath.row]];
    
    return cell;
}

#pragma mark - EVENT

- (void)showDetail:(UIButton *)button {
    
    NSString *key = [NSString stringWithFormat:@"%d",(int)button.tag];
    if (!showManagerDict[key]) {
        showManagerDict[key] = @"yes";
    } else {
        [showManagerDict removeObjectForKey:key];
    }
    
    [myTableView reloadData];
}

#pragma mark - UI

- (UIView *)tableViewHeaderViewAtSection:(NSInteger)section {
    
    NSDictionary *dict = versionArray[section];
    
    CGSize size = [UIScreen mainScreen].bounds.size;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, 45)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 45)];
    [view addSubview:label];
    label.text = [NSString stringWithFormat:@"版本号: %@",dict[@"version"]];
    label.font = [UIFont systemFontOfSize:14];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, size.width - 15, 45)];
    [view addSubview:btn];
    btn.tag = section;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = label.font;
    
    NSString *key = [NSString stringWithFormat:@"%d",(int)section];
    if (!showManagerDict[key]) {
        [btn setTitle:@"展开" forState:UIControlStateNormal];
    } else {
        [btn setTitle:@"收起" forState:UIControlStateNormal];
    }
    
    [btn addTarget:self action:@selector(showDetail:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return view;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
